package com.bkd.dreams.server.volunteer;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;

import com.bkd.dreams.client.event.VolunteerEvent;
import com.bkd.dreams.client.volunteer.Volunteer;
import com.bkd.dreams.client.volunteer.VolunteerDTO;
import com.bkd.dreams.server.PMF;

public class VolunteerHelper {

	public static VolunteerDTO getVolunteerByUserId(String userId) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		VolunteerDTO volunteer = null;
		try {
			List<Volunteer> queryResults = (List<Volunteer>)pm.newQuery("select from " + Volunteer.class.getName() + " where userId == '" + userId + "'").execute();
			if (queryResults.size() == 1) {
				volunteer = new VolunteerDTO(queryResults.get(0));
			}
		} finally {
			pm.close();
		}
		return volunteer;
	}

	public static Volunteer getPersistableVolunteerByUserId(String userId) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Volunteer volunteer = null;
		try {
			List<Volunteer> queryResults = (List<Volunteer>)pm.newQuery("select from " + Volunteer.class.getName() + " where userId == '" + userId + "'").execute();
			if (queryResults.size() == 1) {
				volunteer = queryResults.get(0);
			}
		} finally {
			pm.close();
		}
		return volunteer;
	}

	public static List<VolunteerDTO> getAllVolunteers()
	throws Exception {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		List<VolunteerDTO> volunteers = new ArrayList<VolunteerDTO>();
		try {
			List<Volunteer> queryResults = (List<Volunteer>)pm.newQuery("select from " + Volunteer.class.getName()).execute();
			for(Volunteer vol : queryResults) {
				volunteers.add(new VolunteerDTO(vol));
			}

		} finally {
			pm.close();
		}
		return volunteers;
	}
}
